package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.ActivityTab;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Entity.UserTab;
import com.wze.wze.Service.ActivityTabService;
import com.wze.wze.Impl.ActivityTabImpl;
import com.wze.wze.Util.JSONTool;
import com.wze.wze.Util.RedisTool;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/Activity/")//请求路径
public class ActivityTabController {
    @Resource
    ActivityTabService activityTabService;
    @Resource
    RedisTool redisTool;
    ResultEntity resultEntity = null;

    @GetMapping("Activity")
    private String getActivityById(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            ActivityTab activityTab = activityTabService.getActivityById(id);
            if (activityTab == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(activityTab);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系客服");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 获取信息列表
     * @param page 起始位置
     * @param limit 页码
     * @param id id
     * @param Title 标题
     * @return resultEntity
     */
    @PasToken
    @GetMapping("ActivityList")
    private String ActivityList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("Title") String Title,
            @RequestParam("id") int id
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<ActivityTab> list = activityTabService.getActivityList(Start,limit,id,Title);
            if(id!=-1 || !Title.equals("")){//有条件
                resultEntity.setCount(String.valueOf(list.size()));
            }else {
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PutMapping("Activity")
    private String upActivity(@RequestParam("Context") String Context,@RequestParam("Image") String Image,@RequestParam("Title") String Title, @RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
         ActivityTab activityTab = new ActivityTab();
         activityTab.setId(id);
         activityTab.setTitle(Title);
         activityTab.setImage(Image);
         activityTab.setContext(Context);
            if (activityTabService.upActivity(activityTab) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return json
     */
    @DeleteMapping("Activity")
    private String delActivity(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            if (activityTabService.delActivityById(id) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PostMapping("Activity")
    private String addActivity(@RequestParam("Title") String Title, @RequestParam("Image") String Image,@RequestParam("Context")
            String Context) {
        resultEntity = new ResultEntity();
        try {
            if (activityTabService.addActivity(Title,Image,Context)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
@GetMapping("redis")
    private String Redis(){
    Jedis jedis = redisTool.getJedis();
    jedis.set("code","123456");
    jedis.expire("code",10);
        return "111";
}

}
